#include <iostream>
using namespace std;

//引用及初始化(引用是这块内存的别名,并且不能重新赋值)
void test_left_reference()
{
    int a = 5;
    int  &b = a;//引用必须初始化
    int &c(b);//初始化
    int * &&d = &a; //引用右值
    b = 3;
    cout << "a = " << a << endl;//a = 3
    c = 67;
    cout << "a = " << a << endl;//a = 67
    *d = 56;
    cout << "a = " << a << endl;//a = 56
    int *p = d;//用于对象拷贝

    cout << d << endl;
    cout << &a << endl;
}

//引用作为函数参数,（通过引用cpu直接修改变量的值,不会分配内存来接收参数,没有压栈和出栈操作,效率高)
void change_a(int &a)
{
    a = 34;
    cout << &a << endl;
}

void test_change_a()
{
    int a = 45;
    cout << &a << endl;
    change_a(a);
    cout << "a = " << a << endl;
}

//返回一个引用

int& get_int(int &i)
{
    i = 20;
    return i;
}
//测试返回一个引用
void test_get_int()
{
    int *p = new int(2);
    int a = *p;
    get_int(a);
    cout << a << endl;
    delete(p);
}

//指针的引用
void change_p(int * &p)
{
    p++;
}
//测试指针的引用
void test_change_p()
{
    int *arr = new int[12];
    for (int i = 0; i < 12; i++)
    {
        arr[i] = 10 + i;
    }
    cout << *arr << endl;//10
    change_p(arr);
    cout << *arr << endl;//11
}